/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PFArea.java
 *
 * Created on 2008-12-18, 15:21:24
 */
package impss;

import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 *
 * @author Mikehood
 */
public class PFArea extends javax.swing.JFrame {

    int id = 70001, selectedRow, selectedID;
    Object[][] a;
    Object[] b;
    Object[][] c;
    DBAdapter adapter;
    ResultSet rs;

    public void setID(int i) {
        id = i;
    }

    /** Creates new form PFArea */
    public PFArea() {
        initComponents();
        adapter = new DBAdapter();
    }

    private int produce() {
        int produceid = a[selectedRow][0].hashCode();
        int phoneid = a[selectedRow][1].hashCode();
        int add = a[selectedRow][2].hashCode();
        try {
            String sql = "update produce set produceState='1' where produceID='" + produceid + "';";
            adapter.execute(sql);
            sql = "select phoneSum from phone where phoneID='" + phoneid + "';";
            rs = adapter.executeQuery(sql);
            rs.next();
            int sum = rs.getInt(1) + add;
            sql = "update phone set phoneSum='" + sum + "' where phoneID='" + phoneid + "';";
            adapter.execute(sql);
        } catch (SQLException e) {
            return 1;
        }
        if (!adapter.isSuccessful()) {
            return 1;
        }
        return 0;
    }

    public void setProTable() {
        List<Produce> list = new ArrayList<Produce>();
        try {
            String sql = "select * from produce where(produceState='0' and pfID='" + id + "');";
            rs = adapter.executeQuery(sql);
            while (rs.next()) {
                Produce pf = new Produce();
                pf.setProduceID(rs.getInt("produceID"));
                pf.setPhoneID(rs.getInt("phoneID"));
                pf.setPfNumber(rs.getInt("pfNumber"));
                list.add(pf);
            }
        } catch (SQLException e) {
        }
        int size = list.size();
        a = new Object[size][3];
        for (int i = 0; i < size; i++) {
            a[i][0] = list.get(i).getProduceID();
            a[i][1] = list.get(i).getPhoneID();
            a[i][2] = list.get(i).getPfNumber();
        }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(a, new String[]{"批号", "型号", "数量"}));
    }

    private void setAcTable(int row) {
        List<Accessory> list2 = new ArrayList<Accessory>();
        List<Af> list3 = new ArrayList<Af>();
        try {
            String sql = "select * from accessory where accessoryID in(select accessoryID from structure where phoneID='" + a[row][1] + "');";
            rs = adapter.executeQuery(sql);
            while (rs.next()) {
                Accessory ac = new Accessory();
                ac.setAccessoryID(rs.getInt("accessoryID"));
                ac.setAfID(rs.getInt("afID"));
                ac.setAccessoryPrice(rs.getInt("accessoryPrice"));
                list2.add(ac);
            }
        } catch (SQLException e) {
        }
        int size = list2.size();
        b = new Object[size];
        c = new Object[size][3];
        for (int i = 0; i < size; i++) {
            c[i][0] = list2.get(i).getAccessoryID();
            b[i] = list2.get(i).getAfID();
            c[i][2] = list2.get(i).getAccessoryPrice();
        }
        try {
            String sql = "";
            for (int i = 0; i < size; i++) {
                sql = "select afName from af where(afID='" + b[i] + "');";
                rs = adapter.executeQuery(sql);
                while (rs.next()) {
                    Af af = new Af();
                    af.setAfName(rs.getString(1));
                    list3.add(af);
                }
            }
        } catch (SQLException e) {
        }
        for (int i = 0; i < size; i++) {
            c[i][1] = list3.get(i).getAfName();
        }
        jTable2.setModel(new javax.swing.table.DefaultTableModel(c, new String[]{"配件型号", "制造商", "报价"}));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setTitle("加工");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "批号", "型号", "数量"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("待完成的订单：");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel2.setText("该机型使用的配件：");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "配件型号", "制造商", "报价"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jButton1.setText("制造");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(353, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(195, 195, 195)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(186, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int re = produce();
        if (0 == re) {
            JOptionPane.showMessageDialog(this, "已制造");
            setProTable();
        } else {
            JOptionPane.showMessageDialog(this, "造不出来！");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        selectedRow = jTable1.getSelectedRow();
        setAcTable(selectedRow);
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PFArea().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
